var shares = {};

mui.plusReady(function() {

    plus.share.getServices(function(s) {

        if(s && s.length > 0) {
            for(var i = 0; i < s.length; i++) {
                var t = s[i];
                shares[t.id] = t;
            }
        }
    }, function() {
        //console.log("获取分享服务列表失败");
    });
});

//分享链接点击事件
function share_text(){
    // console.log(test_type);
    var ids = [{
            id: "weixin",
            ex: "WXSceneSession"
        }, {
            id: "weixin",
            ex: "WXSceneTimeline"
        },
            {
                id: "sinaweibo"
            },
            {
                id: "qq"
            }],
        bts = [{
            title: "发送给微信好友"
        }, {
            title: "分享到微信朋友圈"
        },
            {
                title: "分享到新浪微博"
            },
            {
                title: "分享到QQ"
            }];
    plus.nativeUI.actionSheet({			//弹出系统选择按钮框
        cancel: "取消",
        buttons: bts
    }, function(e) {		//点击之后进行回调
        var i = e.index;		//获取点击菜单索引
        if(i > 0) {
            var s_id = ids[i - 1].id;
            ////console.log(shares[s_id]);
            var share = shares[s_id];
            if(share) {
                if(share.authenticated) {		//检测是否授权认证
                    if(test_type == 1){
                        shareMessage(share, ids[i - 1].ex);		//调用发送函数并传入分享对象share、
                    }else{
                        shareMessage2(share, ids[i - 1].ex);		//调用发送函数并传入分享对象share、
                    }
                } else {
                    share.authorize(function() {
                        if(test_type == 1){
                            shareMessage(share, ids[i - 1].ex);
                        }else{
                            shareMessage2(share, ids[i - 1].ex);
                        }
                    }, function(e) {
                        //console.log("认证授权失败：" + e.code + " - " + e.message);
                    });
                }
            } else {
                mui.toast("无法获取分享服务，请检查manifest.json中分享插件参数配置，并重新打包")
            }
        }
    });
}

function shareMessage(share, ex) {
    var msg = {
        extra: {
            scene: ex
        }
    };
    msg.href = "http://api.chinaola.net/ola/test/Index/test";
    if(share_title == "e护理上门-ESRS中风(卒中)风险测试"){
        share_title = "e护上门-ESRS中风(卒中)风险测试";
    }
    msg.title = share_title;
    msg.content = " ";
    if(~share.id.indexOf('sinaweibo')) {
		msg.content = "快来测试您的健康："+share_title;
		msg.thumbs = ["http://api.chinaola.net/ola/Application/Test/Public/images/logo_@2.png"];
	}
	else{
		msg.pictures = ["http://api.chinaola.net/ola/Application/Test/Public/images/logo_@2.png"];
		msg.thumbs = ["http://api.chinaola.net/ola/Application/Test/Public/images/logo_@2.png"];
	}	
    share.send(msg, function() {			//发送分享
        mui.toast("分享到\"" + share.description + "\"成功！ ");
    }, function(e) {
        mui.toast("分享到\"" + share.description + "\"失败: " + e.code + " - " + e.message);
    });
}
function shareMessage2(share, ex) {
    var msg = {
        extra: {
            scene: ex
        }
    };
    msg.href = "http://api.chinaola.net/ola/test/Index/test2";
    msg.content = " ";
    if(share_title == "e护理上门-ESRS中风(卒中)风险测试"){
        share_title = "e护上门-ESRS中风(卒中)风险测试";
    }
    msg.title = share_title;
    if(~share.id.indexOf('sinaweibo')) {
		msg.content = "快来测试您的健康："+share_title;
		msg.thumbs = ["http://api.chinaola.net/ola/Application/Test/Public/images/logo_@2.png"];
	}
	else{
		msg.pictures = ["http://api.chinaola.net/ola/Application/Test/Public/images/logo_@2.png"];
		msg.thumbs = ["http://api.chinaola.net/ola/Application/Test/Public/images/logo_@2.png"];
	}
    share.send(msg, function() {			//发送分享
        mui.toast("分享到\"" + share.description + "\"成功！ ");
    }, function(e) {
        mui.toast("分享到\"" + share.description + "\"失败: " + e.code + " - " + e.message);
    });
}